package com.stellar.auth.controller.admin.authUser;

import cn.hutool.core.util.ObjectUtil;
import com.google.common.base.Preconditions;
import com.stellar.auth.controller.admin.authUser.vo.AuthUserLoginResponseVO;
import com.stellar.auth.controller.admin.authUser.vo.AuthUserRequestPageVO;
import com.stellar.auth.controller.admin.authUser.vo.AuthUserRequestVO;
import com.stellar.auth.controller.admin.authUser.vo.AuthUserResponseVO;
import com.stellar.auth.domain.authUser.AuthUserDomainService;

import com.stellar.core.code.VerifyCode;
import com.stellar.core.service.VerifyCodeService;
import com.stellar.web.core.bean.PageResult;
import com.stellar.web.core.bean.Result;
import com.stellar.web.core.constants.AuthUserConstants;
import com.stellar.web.core.validator.ValidatorRole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统用户信息表控制器
 *
 * @author Jqchan
 * @version 1.0
 * @date 2025-05-16 18:05:29
 */
@RequestMapping("/api-admin/authUser")
@RestController("adminAuthUserController")
@Slf4j
public class AuthUserController {

    @Resource
    private AuthUserDomainService authUserDomainService;

    @Resource
    private VerifyCodeService verifyCodeService;

    /**
     * 管理员登陆
     */
    @PostMapping("/doLogin")
    public Result<AuthUserLoginResponseVO> doLogin(@RequestBody AuthUserRequestVO authUserRequestVO) {
        try {
            Preconditions.checkNotNull(authUserRequestVO, "请求体不能为空");
            Preconditions.checkNotNull(authUserRequestVO.getPhone(), "手机号不能为空");

            return Result.success(authUserDomainService.doLogin(authUserRequestVO));
        } catch (IllegalArgumentException iae) {
            log.error("AuthUserController.doLogin.error", iae.getMessage());
            return Result.fail(iae.getMessage());
        }

    }

    /**
     * 分页查询系统用户信息表
     */
    @ValidatorRole(value = {AuthUserConstants.SUPER_ADMIN, AuthUserConstants.SCHOOL_ADMIN})
    @PostMapping("/page")
    public Result<PageResult<AuthUserResponseVO>> page(@RequestBody AuthUserRequestPageVO requestPageVO) {
        return Result.success(authUserDomainService.page(requestPageVO));
    }

    /**
     * 新增系统用户信息表
     */
    @ValidatorRole
    @PostMapping("/create")
    public Result<Boolean> create(@RequestBody AuthUserRequestVO requestVO) {
        try {
            if (log.isInfoEnabled()) {
                Preconditions.checkNotNull(requestVO, "请求体不能为空");
                Preconditions.checkNotNull(requestVO.getNickName(), "微信昵称不能为空");
                Preconditions.checkNotNull(requestVO.getSchoolId(), "学校Id不能为空");
                Preconditions.checkNotNull(requestVO.getRoleId(), "角色不能为空");
                Preconditions.checkNotNull(requestVO.getPhone(), "手机号不能为空");
            }
            Long userId = authUserDomainService.insert(requestVO);
            return Result.success(ObjectUtil.isNotNull(userId));
        } catch (IllegalArgumentException iae) {
            log.error("AuthUserController.create.error:{}", iae.getMessage());
            return Result.fail(iae.getMessage());
        }
    }

    /**
     * 修改系统用户信息表
     */
    @ValidatorRole
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthUserRequestVO requestVO) {
        try {
            if (log.isInfoEnabled()) {
                Preconditions.checkNotNull(requestVO, "请求体不能为空");
                Preconditions.checkNotNull(requestVO.getId(), "Id不能为空");
                Preconditions.checkNotNull(requestVO.getNickName(), "微信昵称不能为空");
                Preconditions.checkNotNull(requestVO.getOpenid(), "OpenId不能为空");
                Preconditions.checkNotNull(requestVO.getSchoolId(), "学校Id不能为空");
                Preconditions.checkNotNull(requestVO.getPhone(), "手机号不能为空");
            }
            return Result.success(authUserDomainService.update(requestVO));
        } catch (IllegalArgumentException iae) {
            log.error("AuthUserController.update.error:{}", iae.getMessage());
            return Result.fail(iae.getMessage());
        }
    }

    /**
     * 删除系统用户信息表
     */
    @ValidatorRole
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return Result.success(authUserDomainService.delete(id));
    }

    /**
     * 获取系统用户信息表详情
     */
    @ValidatorRole(value = {AuthUserConstants.SUPER_ADMIN, AuthUserConstants.SCHOOL_ADMIN})
    @GetMapping("/get/{id}")
    public Result<AuthUserResponseVO> get(@PathVariable("id") Long id) {
        return Result.success(authUserDomainService.get(id));
    }


    /**
     * 获取系统用户信息表详情 - 通过上下文获取用户Id
     */
    @ValidatorRole(value = {AuthUserConstants.SUPER_ADMIN, AuthUserConstants.SCHOOL_ADMIN})
    @GetMapping("/getUserInfo")
    public Result<AuthUserResponseVO> getUserInfo() {
        return Result.success(authUserDomainService.getUserInfo());
    }


    /**
     * 启用/封禁用户
     */
    @ValidatorRole(value = {AuthUserConstants.SUPER_ADMIN, AuthUserConstants.SCHOOL_ADMIN})
    @PostMapping("/toggleStatus/{id}")
    public Result<Boolean> toggleStatus(@PathVariable("id") Long id) {
        return Result.success(authUserDomainService.toggleStatus(id));
    }

    /**
     * 手机电话查询用户
     */
    @ValidatorRole()
    @GetMapping("/getByPhone/{phone}")
    public Result<AuthUserResponseVO> getByPhone(@PathVariable("phone") String phone) {
        return Result.success(authUserDomainService.getByPhone(phone));
    }

    /**
     * 批量查询用户信息
     */
    @ValidatorRole()
    @PostMapping("/batchQueryByIds")
    public Result<List<AuthUserResponseVO>> batchQueryByIds(@RequestBody List<Long> ids) {
        return Result.success(authUserDomainService.batchQueryByIds(ids));
    }

    /**
     * 获取用户数量通过学校Id
     */
    @ValidatorRole(value = {AuthUserConstants.SUPER_ADMIN, AuthUserConstants.SCHOOL_ADMIN})
    @GetMapping("/countBySchoolId/{id}")
    public Result<Long> countBySchoolId(@PathVariable("id") Long id) {
        return Result.success(authUserDomainService.countBySchoolId(id));
    }

    /**
     * 发送手机短信
     */
    @PostMapping("/sendSms")
    public Result<VerifyCode> sendSms(@RequestParam("phone") String phone) {
        try {
            VerifyCode verifyCode = verifyCodeService.generateSmsCode(phone);
            if (verifyCode != null) {
                return Result.success("验证码发送成功");
            } else {
                return Result.fail("验证码发送失败");
            }
        } catch (IllegalArgumentException iae) {
            log.error("AuthUserController.sendSms.error:{}", iae.getMessage());
            return Result.fail(iae.getMessage());
        }
    }
}
